package com.cole.gx095.service.impl;

import com.cole.gx095.Repository.PurchasecontractRepository;
import com.cole.gx095.domain.PurQuery;
import com.cole.gx095.service.PurQueryService;
import com.cole.gx095.entity.Purchasecontract;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class PurQueryServiceImpl implements PurQueryService {

    @Autowired
    PurchasecontractRepository purchasecontractRepository;

    /**
     * typeName
     * goodsName
     * 两个没有实现 涉及多表
     * 直接用sql 如何动态 不会？
     * @param purQuery
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<Purchasecontract> findAllBy(PurQuery purQuery, Integer currentPage, Integer pageSize) {
        currentPage--;
        currentPage = currentPage < 0 ? 0 : currentPage;//page 为页码，数据库从0页开始
        //可以使用重载的 of(int page, int size, Sort sort) 方法指定排序字段
        Pageable pageable = PageRequest.of(currentPage, pageSize);

        Specification<Purchasecontract> purspecification = new Specification<Purchasecontract>() {
            @Override
            public Predicate toPredicate(Root<Purchasecontract> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
//                Join<PurBillgoods,Purchasecontract> join = root.join("purchasecontract", JoinType.LEFT);
//                Join<Purchasecontract,PurBillgoods> join = root.join("purBillgoods", JoinType.INNER);
                if (purQuery.getContractNum()!=null&&purQuery.getContractNum()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("contractNum").as(String.class),purQuery.getContractNum()));
                if (purQuery.getContractDate()!=null)
                    predicateList.add(criteriaBuilder.equal(root.get("contractDate").as(Date.class),purQuery.getContractDate()));
                if (purQuery.getSupplier()!=null&&!"".equals(purQuery.getSupplier()))
                    predicateList.add(criteriaBuilder.equal(root.get("supplier").as(String.class),purQuery.getSupplier()));
                if (purQuery.getBillStatus()!=null)
                    predicateList.add(criteriaBuilder.equal(root.get("billStatus").as(Integer.class),purQuery.getBillStatus()));
                if (purQuery.getRemarks()!=null&&!"".equals(purQuery.getRemarks()))
                    predicateList.add(criteriaBuilder.equal(root.get("remarks").as(String.class),purQuery.getRemarks()));
//                if (purQuery.getGoodsName()!=null&&!"".equals(purQuery.getGoodsName()))
//                    predicateList.add(criteriaBuilder.equal(join.get("goodsName"), purQuery.getGoodsName()));
//                if (purQuery.getModle()!=null&&!"".equals(purQuery.getModle()))
//                    predicateList.add(criteriaBuilder.equal(join.get("modle"), purQuery.getModle()));


                Predicate[] predicates = new Predicate[predicateList.size()];
                return query.where(predicateList.toArray(predicates)).getRestriction();
            }
        };
        return purchasecontractRepository.findAll(purspecification,pageable);//无数据时返回空列表;
    }
}
